<?php

session_start();
include("db_conn.php");
include("Narrower.php");

if (!isset($_SESSION['narrower']) || 1==1) {
	$n = &new Narrower("GET");
	
	$typeId = $_GET['t'];
	
	// CA
	if ($typeId == '1') {
	
		$sql1 = "SELECT countyList FROM caprofile WHERE caId='17'|SELECT county, code FROM countycodes WHERE code = 'XXX' order by county asc";
		$ajaxSQL = "SELECT countyList FROM provider where providerId='XXX0' AND caId='17'|SELECT county, code FROM countycodes WHERE code = 'XXX' order by county asc";
		$n->add_form_element("Counties", "comma", $sql1, $ajaxSQL, "a.geoService_1");
	
		$n->add_form_element("Activity Date", "dates", "NOT NEEDED", "NOT NEEDED", "a.activityDate");
		
		$sql1 = "SELECT distinct a.groupId, b.grpName FROM activities as a, groups as b WHERE a.caId='17' and b.groupId = a.groupId order by b.grpName";
		$ajaxSQL = "SELECT distinct a.groupId, b.grpName	FROM activities as a, groups as b WHERE a.caId='17' and 'param0'='param0' and a.providerId='XXX0' and 'param1'='param1' and a.natureOfEvent='XXX1' and b.groupId = a.groupId order by b.grpName";
		$n->add_form_element("Group Names", "dropdown", $sql1, $ajaxSQL, "a.groupId");
		
		$sql1 = "SELECT codeId, codeDesc from natureofevent";
		$n->add_form_element("Group Types", "dropdown", $sql1, "NOT NEEDED", "g.type");
		
		$sql1 = "SELECT distinct a.providerId, p.providerName, p.providerId from activities as a, provider as p where a.caId='17' AND a.providerId = p.providerId order by providerName asc";
		$n->add_form_element("Providers", "dropdown", $sql1, "NOT NEEDED", "a.providerId");
		
		$sql1 = "SELECT distinct staffId, CONCAT(firstName, ' ', lastName) FROM users WHERE entityId IN (SELECT providerId FROM provider WHERE caId='17') order by firstName asc";
		$ajaxSQL = "SELECT distinct staffId, CONCAT(firstName, ' ', lastName) FROM users WHERE entityId = 'XXX0' order by firstName asc";
		$n->add_form_element("Staff", "dropdown", $sql1, $ajaxSQL, "a.staffId::r");
		
		$n->add_form_element("Record Id", "number", "NOT NEEDED", "NOT NEEDED", "a.recordNum");
		
		$n->add_form_element("Entry Date", "dates", "NOT NEEDED", "NOT NEEDED", "a.created");
	
		$n->tie("Providers", "Staff");
		$n->tie("Providers", "Counties");
		$n->tie("Providers", "Group Names");
		
		$n->tie("Group Types", "Group Names");
	} else if ($typeId == '2') {

		$sql1 = "SELECT countyList FROM provider WHERE providerId='31'|SELECT county, code FROM countycodes WHERE code = 'XXX' order by county asc";
		$n->add_form_element("Counties", "comma", $sql1, "NOT NEEDED", "a.geoService_1");

		$n->add_form_element("Activity Date", "dates", "NOT NEEDED", "NOT NEEDED", "a.activityDate");

		$sql1 = "SELECT distinct a.groupId, b.grpName FROM activities as a, groups as b WHERE a.providerId='31' and b.groupId = a.groupId order by b.grpName";
		// DOESNT WORK
		$ajaxSQL = "SELECT distinct a.groupId, b.grpName FROM activities as a, groups as b WHERE a.providerId='31' and a.natureOfEvent='XXX0' and b.groupId = a.groupId order by b.grpName";
		$n->add_form_element("Group Names", "dropdown", $sql1, $ajaxSQL, "a.groupId");
	
		$sql1 = "SELECT codeId, codeDesc from natureofevent";
		$n->add_form_element("Group Types", "dropdown", $sql1, "NOT NEEDED", "g.type");
	
		$sql1 = "SELECT distinct staffId, CONCAT(firstName, ' ', lastName) FROM users WHERE entityId ='31' order by firstName asc";
		$n->add_form_element("Staff", "dropdown", $sql1, "NOT NEEDED", "a.staffId::r");

		$n->add_form_element("Record Id", "number", "NOT NEEDED", "NOT NEEDED", "a.recordNum");
		
		$n->add_form_element("Entry Date", "dates", "NOT NEEDED", "NOT NEEDED", "a.created");

		$n->tie("Group Types", "Group Names");
		
	} else if ($typeId == '4') {

		$sql1 = "SELECT code, county FROM countycodes order by county asc";
		$ajaxSQL = "SELECT countyList FROM provider where providerId='XXX0'|SELECT county, code FROM countycodes WHERE code = 'XXX' order by county asc";
		$n->add_form_element("Counties", "commafusion", $sql1, $ajaxSQL, "a.geoService_1");
		
		$n->add_form_element("Activity Date", "dates", "NOT NEEDED", "NOT NEEDED", "a.activityDate");
	
		$sql1 = "SELECT distinct a.groupId, b.grpName FROM activities as a, groups as b WHERE a.caId='17' and b.groupId = a.groupId order by b.grpName";
		$ajaxSQL = "SELECT distinct a.groupId, b.grpName FROM activities as a, groups as b WHERE 'param0'='param0' and a.providerId='XXX0' and 'param1'='param1' and a.natureOfEvent='XXX1' and b.groupId = a.groupId order by b.grpName";
		$n->add_form_element("Group Names", "dropdown", $sql1, $ajaxSQL, "a.groupId");
	
		$sql1 = "SELECT codeId, codeDesc from natureofevent";
		$n->add_form_element("Group Types", "dropdown", $sql1, "NOT NEEDED", "g.type");
	
		$sql1 = "SELECT distinct a.caId, c.name from activities as a, caprofile as c where a.caId = c.caId order by c.name asc";
		$n->add_form_element("CA", "dropdown", $sql1, "NOT NEEDED", "a.caId");

		$sql1 = "SELECT distinct a.providerId, p.providerName, p.providerId from activities as a, provider as p where a.providerId = p.providerId order by providerName asc";
		$ajaxSQL = "SELECT distinct a.providerId, p.providerName, p.providerId from activities as a, provider as p where a.caId='XXX0' AND a.providerId = p.providerId order by providerName asc";
		$n->add_form_element("Providers", "dropdown", $sql1, $ajaxSQL, "a.providerId");
	
	
		$sql1 = "SELECT distinct staffId, CONCAT(firstName, ' ', lastName) FROM users WHERE entityId IN (SELECT providerId FROM provider) order by firstName asc";
		$ajaxSQL = "SELECT distinct staffId, CONCAT(firstName, ' ', lastName) FROM users WHERE entityId = 'XXX0' order by firstName asc";
		$n->add_form_element("Staff", "dropdown", $sql1, $ajaxSQL, "a.staffId::r");
	
		$n->add_form_element("Record Id", "number", "NOT NEEDED", "NOT NEEDED", "a.recordNum");
		
		$n->add_form_element("Entry Date", "dates", "NOT NEEDED", "NOT NEEDED", "a.created");

		$n->tie("CA", "Providers");

		$n->tie("Providers", "Staff");
		$n->tie("Providers", "Counties");
		$n->tie("Providers", "Group Names");

		$n->tie("Group Types", "Group Names");
	}
	
	$_SESSION['narrower'] = serialize($n);
} else {
	$n = unserialize($_SESSION['narrower']);
}

$n->ajax();
?>

<html>
<head>
	<script type='text/javascript' src='js/jquery.js'></script>
	<script type='text/javascript' src='js/jquery.ui.js'></script>
	
	<link rel="stylesheet" href="css/jquery-ui-themeroller.css" type="text/css" media="screen" title="Flora (Default)">
	
	
</head>
<body>
<?php
	if ($n->posted()) {
		echo $n->process();
	}
	
	$n->create_form();

	// This needs to be the last line
	//$_SESSION['narrower'] = serialize($n);
?>
</body>
</html>